/*
 Copyright (C) 2013 Yuyao Liang

 This source code is release under the BSD License.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
 

 */
package com.googlecode.stereoplot;

import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisSpace;
import org.jfree.chart.axis.ColorBar;
import org.jfree.chart.event.PlotChangeEvent;
import org.jfree.chart.plot.PlotRenderingInfo;
import org.jfree.chart.plot.PlotState;
import org.jfree.chart.plot.dial.DialPlot;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;

/**
 * @author lianyuy
 * 
 */
@SuppressWarnings("deprecation")
public class StereoDialPlot extends DialPlot {
	private static final long serialVersionUID = 1333050151912296619L;

	private StereoPlotDataQuickSeries stereoPlotDataQuickSeries;
	private ColorBar colorBar;
	private RectangleEdge colorBarLocation;

	public StereoDialPlot() {
	}

	/**
	 * @param dataset
	 */
	public StereoDialPlot(StereoPlotDataQuickSeries stereoPlotDataQuickSeries,
			ColorBar colorBar) {
		this.stereoPlotDataQuickSeries = stereoPlotDataQuickSeries;
		this.colorBar = colorBar;
		if (colorBar != null) {
			colorBar.getAxis().setPlot(this);
			colorBar.getAxis().addChangeListener(this);
		}
		this.colorBarLocation = RectangleEdge.BOTTOM;

	}

	/**
	 * Returns the color bar location.
	 * 
	 * @return The color bar location.
	 */
	public RectangleEdge getColorBarLocation() {
		return this.colorBarLocation;
	}

	/**
	 * Sets the color bar location and sends a {@link PlotChangeEvent} to all
	 * registered listeners.
	 * 
	 * @param edge
	 *            the location.
	 */
	public void setColorBarLocation(RectangleEdge edge) {
		this.colorBarLocation = edge;
		fireChangeEvent();
	}

	/**
	 * @return the stereoPlotDataSeries
	 */
	public StereoPlotDataQuickSeries getStereoPlotDataQuickSeries() {
		return stereoPlotDataQuickSeries;
	}

	/**
	 * @param stereoPlotDataSeries
	 *            the stereoPlotDataSeries to set
	 */
	public void setStereoPlotDataQuickSeries(
			StereoPlotDataQuickSeries stereoPlotDataSeries) {
		this.stereoPlotDataQuickSeries = stereoPlotDataSeries;
	}

	/**
	 * Draws the plot. This method is usually called by the {@link JFreeChart}
	 * instance that manages the plot.
	 * 
	 * @param g2
	 *            the graphics target.
	 * @param area
	 *            the area in which the plot should be drawn.
	 * @param anchor
	 *            the anchor point (typically the last point that the mouse
	 *            clicked on, <code>null</code> is permitted).
	 * @param parentState
	 *            the state for the parent plot (if any).
	 * @param info
	 *            used to collect plot rendering info (<code>null</code>
	 *            permitted).
	 */
	@Override
	public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor,
			PlotState parentState, PlotRenderingInfo info) {

		// if the plot area is too small, just return...
		boolean b1 = (area.getWidth() <= MINIMUM_WIDTH_TO_DRAW);
		boolean b2 = (area.getHeight() <= MINIMUM_HEIGHT_TO_DRAW);
		if (b1 || b2) {
			return;
		}

		// record the plot area...
		if (info != null) {
			info.setPlotArea(area);
		}

		// adjust the drawing area for plot insets (if any)...
		RectangleInsets insets = getInsets();
		insets.trim(area);
		AxisSpace space = new AxisSpace();
		Rectangle2D estimatedDataArea = space.shrink(area, null);
		AxisSpace space2 = new AxisSpace();
		space2 = this.colorBar.reserveSpace(g2, this, area, estimatedDataArea,
				this.colorBarLocation, space2);
		Rectangle2D adjustedPlotArea = space2.shrink(area, null);
		Rectangle2D dataArea = space.shrink(adjustedPlotArea, null);
		Rectangle2D colorBarArea = space2.reserved(area, this.colorBarLocation);

		double cursor = dataArea.getMaxY();

		colorBar.draw(g2, cursor, adjustedPlotArea, dataArea, colorBarArea,
				this.colorBarLocation);
		super.draw(g2, dataArea, anchor, parentState, info);

	}

}
